<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="车牌号" prop="carLicenseNumber">
        <el-input
          v-model="queryParams.carLicenseNumber"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车主姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入车主姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车主电话" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入车主电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:parkCar:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:parkCar:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:parkCar:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:parkCar:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkCarList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="汽车表id" align="center" prop="id" v-if="false"/>
      <el-table-column label="车主姓名" align="center" prop="userName" />
      <el-table-column label="车主电话" align="center" prop="userPhone" />
      <el-table-column label="车主身份证号" align="center" prop="userIdcard" width="145"/>
      <el-table-column label="车牌号" align="center" prop="carLicenseNumber" />
      <el-table-column label="车牌颜色" align="center" prop="carLicenseColor" :formatter="carLicenseColorFormat" />
      <el-table-column label="车牌类型" align="center" prop="carLicenseType" :formatter="carLicenseTypeFormat" />
      <el-table-column label="车辆颜色" align="center" prop="carColor" :formatter="colorFormat"/>
      <el-table-column label="车辆品牌" align="center" prop="carLogo" />
      <el-table-column label="品牌型号" align="center" prop="carModel" />
      <el-table-column label="车辆图片" align="center" prop="carFullPicture" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-picture"  v-if="scope.row.carFullPicture"
            @click="showDetailUrl(scope.row.carFullPicture)"
          >点击查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="车辆类型" align="center" prop="carType" :formatter="carTypeFormat" />
      <el-table-column label="车归属类型" align="center" prop="carOwnType" :formatter="carOwnTypeFormat" />
      <el-table-column label="添加操作人" align="center" prop="addBy" />
      <el-table-column label="车辆大小类型" align="center" prop="carSizeType" :formatter="carSizeTypeFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:parkCar:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:parkCar:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 图片列表框 -->
    <el-dialog :title="imgTitle" :visible.sync="imgListOpen" v-if="imgListOpen" width="60%" append-to-body>
      <show-imgs :imgs="imgUrlList"></show-imgs>
    </el-dialog>

    <!-- 添加或修改车辆管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true"  label-width="120px">
        <el-form-item label="车牌号" prop="carLicenseNumber">
          <el-input v-model="form.carLicenseNumber" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车主姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入车主姓名" />
        </el-form-item>
        <el-form-item label="车主电话" prop="userPhone">
          <el-input v-model="form.userPhone" placeholder="请输入车主电话" />
        </el-form-item>
        <el-form-item label="车主身份证号" prop="userIdcard">
          <el-input v-model="form.userIdcard" placeholder="请输入车主身份证号" />
        </el-form-item>

        <el-form-item label="车辆品牌" prop="carLogo">
          <el-select v-model="form.carLogo" clearable filterable placeholder="请选择车辆品牌" style="width: 203px">
            <el-option
              v-for="cars in carsBrandList"
              :key="cars.brand_name"
              :label="cars.brand_name"
              :value="cars.brand_name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌型号" prop="carModel">
          <el-input v-model="form.carModel" placeholder="请输入品牌型号" />
        </el-form-item>
        <el-form-item label="车牌颜色" prop="carLicenseColor">
          <el-select v-model="form.carLicenseColor"  style="width: 203px" clearable  placeholder="请选择车牌颜色">
            <el-option
              v-for="dict in carLicenseColorOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆颜色" prop="carColor">
          <el-select v-model="form.carColor"  style="width: 203px" clearable  placeholder="请选择车辆颜色">
            <el-option
              v-for="dict in colorOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车牌类型" prop="carLicenseType">
          <el-select v-model="form.carLicenseType"  style="width: 203px" clearable  placeholder="请选择车牌类型">
            <el-option
              v-for="dict in carLicenseTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆类型" prop="carType">
          <el-select v-model="form.carType"  style="width: 203px" clearable  placeholder="请选择车辆类型">
            <el-option
              v-for="dict in carTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车归属类型" prop="carOwnType">
          <el-select v-model="form.carOwnType"  style="width: 203px" clearable  placeholder="请选择车归属类型">
            <el-option
              v-for="dict in carOwnTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆大小类型" prop="carSizeType">
          <el-select v-model="form.carSizeType" style="width: 203px" clearable placeholder="请选择车辆大小类型">
            <el-option
              v-for="dict in carSizeTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆图片">
          <upload-img v-model="form.carFullPicture" :limit="9"></upload-img>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listParkCar, getParkCar, delParkCar, addParkCar, updateParkCar, exportParkCar } from "@/api/system/parkCar";
import moreImgUpload from '@/components/moreImgUpload';
import UploadImg from "@/components/UploadImg";
import ShowImgs from "@/components/ShowImgs";

export default {
  name: "ParkCar",
  components: {
    ShowImgs,
    UploadImg,
    moreImgUpload,
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆管理表格数据
      parkCarList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      imgListOpen: false,
      imgTitle: "",
      imgUrlList: "",
      // 车牌颜色字典
      carLicenseColorOptions: [],
      // 车辆颜色字典
      colorOptions: [],
      // 车牌类型字典
      carLicenseTypeOptions: [],
      // 车辆类型字典
      carTypeOptions: [],
      //车辆品牌列表
      carsBrandList: [],
      // 车归属类型字典
      carOwnTypeOptions: [],
      // 车辆大小类型字典
      carSizeTypeOptions: [],
      // 是否逻辑删除，0未删除，1删除字典
      isDeletedOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        communityId: undefined,
        customerId: undefined,
        carLicenseNumber: undefined,
        carLicenseColor: undefined,
        carLicenseType: undefined,
        carColor: undefined,
        carLogo: undefined,
        carModel: undefined,
        carType: undefined,
        carOwnType: undefined,
        addTime: undefined,
        addBy: undefined,
        carSizeType: undefined,
        similarPlates: undefined,
        isDeleted: undefined,
        createtime: undefined,
        updatetime: undefined,
        userName: undefined,
        userPhone: undefined,
        userIdcard: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carLicenseNumber:[
          {required:true,message:"请输入车牌号!",trigger:"blur"}
        ],
        userName:[
          {required:true,message:"请输入车主姓名!",trigger:"blur"}
        ],
        userIdcard:[
          {required:true,message:"请输入车主身份证号!",trigger:"blur"}
        ],
        userPhone:[
          {required:true,message:"请输入正确的手机号",trigger:"blur",pattern: /^1[3456789]\d{9}$/}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCarsBrandList();
    this.getDicts("parking_car_license_color").then(response => {
      this.carLicenseColorOptions = response.data;
    });
    this.getDicts("park_car_color").then(response => {
      this.colorOptions = response.data;
    });
    this.getDicts("parking_car_license_type").then(response => {
      this.carLicenseTypeOptions = response.data;
    });
    this.getDicts("car_size_type").then(response => {
      this.carTypeOptions = response.data;
    });
    this.getDicts("car_own_type").then(response => {
      this.carOwnTypeOptions = response.data;
    });
    this.getDicts("car_size_type").then(response => {
      this.carSizeTypeOptions = response.data;
    });
    this.getDicts("sys_is_delete").then(response => {
      this.isDeletedOptions = response.data;
    });
  },
  methods: {
    /** 查询车辆管理列表 */
    getList() {
      this.loading = true;
      listParkCar(this.queryParams).then(response => {
        this.parkCarList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取车辆品牌
    getCarsBrandList(){
      var  brandList = require('./brand.json');
      this.carsBrandList = brandList.result;
    },
    // 车牌颜色字典翻译
    carLicenseColorFormat(row, column) {
      return this.selectDictLabel(this.carLicenseColorOptions, row.carLicenseColor);
    },
    // 车牌类型字典翻译
    carLicenseTypeFormat(row, column) {
      return this.selectDictLabel(this.carLicenseTypeOptions, row.carLicenseType);
    },
    // 车辆类型字典翻译
    carTypeFormat(row, column) {
      return this.selectDictLabel(this.carTypeOptions, row.carType);
    },
    // 车辆颜色字典翻译
    colorFormat(row, column) {
      return this.selectDictLabel(this.colorOptions, row.carColor);
    },
    // 车归属类型字典翻译
    carOwnTypeFormat(row, column) {
      return this.selectDictLabel(this.carOwnTypeOptions, row.carOwnType);
    },
    // 车辆大小类型字典翻译
    carSizeTypeFormat(row, column) {
      return this.selectDictLabel(this.carSizeTypeOptions, row.carSizeType);
    },
    // 是否逻辑删除，0未删除，1删除字典翻译
    isDeletedFormat(row, column) {
      return this.selectDictLabel(this.isDeletedOptions, row.isDeleted);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.imgListOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        communityId: undefined,
        customerId: undefined,
        carLicenseNumber: undefined,
        carLicenseColor: undefined,
        carLicenseType: undefined,
        carColor: undefined,
        carLogo: undefined,
        carModel: undefined,
        carFullPicture: undefined,
        carType: undefined,
        carOwnType: undefined,
        addTime: undefined,
        addBy: undefined,
        carSizeType: undefined,
        similarPlates: undefined,
        isDeleted: 0,
        createtime: undefined,
        updatetime: undefined,
        userName: undefined,
        userPhone: undefined,
        userIdcard: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //车辆图片
    showDetailUrl(imgUrl){
      this.imgListOpen = true;
      this.imgTitle = "车辆图片";
      this.imgUrlList = imgUrl;

    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getParkCar(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (this.isVehicleNumber(this.form.carLicenseNumber) == false){
            this.msgError("请输入正确的车牌号!");
            return false;
        }
        if (this.checkIDCard(this.form.userIdcard) == false){
            this.msgError("请输入正确的身份证号!");
            return false;
        }
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateParkCar(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParkCar(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除车辆管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delParkCar(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有车辆管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportParkCar(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
